burstid=$1
t0=$2
dir_csc="/disk2/test/GALTest/testdata/scenario1/"
dir_bakfile="/disk2/test/xlp2/GALtest/sc1/"
datetime=`date -u +%Y%m%d`
curTime=`date -u +%F"T"%T`
#ls *.txt >lista
str2jd $t0 >tmp1
triggerJD=`tail -1 tmp1 | awk '{print($3)}'`

xm ( )
{
	
                echo "#- delimiter is = , split parameter with value. " >scienceProducts.txt
                curTime=`date -u +%F"T"%T`
                echo "#CREATTIME="$curTime >>scienceProducts.txt
                echo "#FINDING=$finding" >>scienceProducts.txt
                echo "#UPMAG=$upmag" >>scienceProducts.txt
                echo "#OBSTYPE=GRB-INIT" >>scienceProducts.txt
                echo "#BURST_ID="$burstid >>scienceProducts.txt
                echo "#OBS_ID=2566914148" >>scienceProducts.txt
                echo "#ORIGIN=CSC" >>scienceProducts.txt
                echo "#TRIG_MJD="$triggerJD >>scienceProducts.txt
                echo "#FILTER="$filter >>scienceProducts.txt
                echo "#RA_C=169.778" >>scienceProducts.txt
                echo "#DEC_C=3.107" >>scienceProducts.txt
                echo "#-delta, exp, date-obs, filter, mag, merr, limitmag, ra, dec, xc, yc, distance, OTNAME, trigger_ID, image, telescope" >>scienceProducts.txt
                echo "#-delta, exp, date-obs, filter, mag, merr, limitmag, ra, dec, xc, yc, distance, IDCAND, BURST_ID(TOOMM_ID), TILENUM(default=0), IMAGEID, Telescope,fchart, alt_id, trig_name" >>scienceProducts.txt
		echo $obstring >>scienceProducts.txt

}

xsend2BA ( )
{

#lctabler="sb24011700_OTsb22012454_20240117_r_001.fit.lc"
#lctableg="sb24011700_OTsb22012454_20240117_g_001.fit.lc"
lctabler=`echo $burstid"_OT"$burstid"_"$curTime"_r_001.fit.lc"`
lctableg=`echo $burstid"_OT"$burstid"_"$curTime"_g_001.fit.lc"`
cp sb24011700_OTsb22012454_20240117_r_001.fit.lc $lctabler
cp sb24011700_OTsb22012454_20240117_g_001.fit.lc $lctableg

#imgparamr="OTsb22012454_20240117_r_001.param"
#imgparamg="OTsb22012454_20240117_g_001.param"
imgparamr=`echo "OT"$burstid"_"$curTime"_r_001.param"`
imgparamg=`echo "OT"$burstid"_"$curTime"_g_001.param"`
cp OTsb22012454_20240117_r_001.param $imgparamr
cp OTsb22012454_20240117_g_001.param $imgparamg
lcparam=`echo $burstid".lcp"`
cp sb24011700.lcp $lcparam
TriggerID=`echo $burstid`
telescope="CGFT"
dateobs=`echo $curTime`
echo "python3  ~/software/softgft/sendFile.py  imageparam  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $imgparamr"
python3  ~/software/softgft/sendFile.py  imageparam  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $imgparamr
python3  ~/software/softgft/sendFile.py  imageparam  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $imgparamg

 python3  ~/software/softgft/sendFile.py  lightcurveparam  $TriggerID  001 002 $telescope  $dateobs  $telescope   pipeline  01 test1 $lcparam

 python3  ~/software/softgft/sendFile.py  lightcurve  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $lctabler
 python3  ~/software/softgft/sendFile.py  lightcurve  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $lctableg
cp $lctableg $lctableg $lcparam $imgparamg $imgparamr $OT*.txt $dir_bakfile
}

for file1 in `cat lista`
do
	filter=`cat $file1 | grep FILTER | tr '=' ' ' | awk '{print($2)}'`
	echo $filter
	filename=`echo "OT"$burstid"_"$datetime"_"$filter"_001_forCSC.txt"`
	echo $filename
	if [ "$filter"x == "g"x   ]
	then
		upmag="None"
		finding="True"
		obstring=`echo "180 10 2024-01-17T13:03:00 g 15.6012 0.100 18.1554 169.77739 3.10851 1035.011 1087.111 27.568 gJ169.77739+3.10851 $burstid 0 OTsb22012454_20240117_g_001 CGFT sb24011700/OTsb22012454_20240117_g_001_gJ169.77739+3.10851_cut.png 12341000 $burstid"`
	elif [ "$filter"x == "r"x   ]
	then
		upmag="None"
		finding="True"
		obstring=`echo "180 10 2024-01-17T13:03:00 r 14.3239 0.025 18.5123 169.77771 3.10868 1047.000 1080.292 27.848 rJ169.77771+3.10868 $burstid 0 OTsb22012454_20240117_r_001 CGFT sb24011700/OTsb22012454_20240117_r_001_rJ169.77771+3.10868_cut.png 12341000  $burstid"`
	else
		upmag=17.7916
		finding="False"
		obstring=`echo "180 10 2024-01-17T13:03:00 i 999 999 17.7916 999 999 999 999 999 999 $burstid 0 OTsb22012454_20240117_i_001 CGFT 999 12341000 $burstid"`
	fi
	xm
	mv scienceProducts.txt $filename
	cp $filename $dir_csc
done

xsend2BA  
